<?php

namespace App\Console\Commands;

use App\Models\Setting\ExchangeCode;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;

class GenerateExchangeCode extends Command
{

    protected $signature = 'code';

    public function handle() {

        $codeMap = ExchangeCode::pluck('code')->toArray();

        $map = [
            'exfirst'=>1,
        ];
        $time = date('Y-m-d H:i:s');
        $records = [];
        foreach ($map as $k=>$count) {
            foreach (range(1, $count) as $i){
                $code = strtolower(Str::random(6));
                while (in_array($code, $codeMap)){
                    $code = strtolower(Str::random(6));
                }
                $records[] = [
                    'code'=>$code,
                    'type'=>$k,
                    'created_at'=>$time,
                    'updated_at'=>$time,
                ];
            }
        }

        ExchangeCode::insert($records);
        return 0;
    }
}
